package com.tanhua.dubbo.api;

import com.tanhua.model.db.RecommendUser;
import com.tanhua.model.vo.TodayBest;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @auther : 白月尘
 * @date : 12:43  2021/10/20
 */
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).skip(0).limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public List<RecommendUser> findAll(long userId, Integer page, Integer pageSize) {
        Criteria criteria = Criteria.where("toUserId").is(userId);
        Query query = new Query(criteria).with(Sort.by(Sort.Order.desc("score"))).skip((page - 1) * pageSize).limit(pageSize);
        return mongoTemplate.find(query, RecommendUser.class);
    }

    @Override
    public RecommendUser findById(long userId, long toUserId) {
        Criteria criteria = Criteria.where("userId").is(toUserId).and("toUserId").is(userId);
        RecommendUser recommendUser = mongoTemplate.findOne(Query.query(criteria), RecommendUser.class);
        if (recommendUser==null){
            recommendUser=new RecommendUser();
            recommendUser.setScore(95d);
            recommendUser.setUserId(toUserId);
            recommendUser.setToUserId(userId);
        }
        return recommendUser;
    }
}